package com.axend.aerosense.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModel;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoginStatusChanged;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.adapter.CommonBindingAdapters;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.router.RouterFragmentPath;
import com.axend.aerosense.common.ui.WebActivity;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.UserSettingActivity;
import com.axend.aerosense.user.databinding.UserMainFragmentBinding;

@Route(path = RouterFragmentPath.User.PAGE_MAIN_USER)
public class UserMainFragment extends MvvmBaseFragment<UserMainFragmentBinding, ViewModel>
        implements ILoginStatusChanged {
    private UserInfo userInfo;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
    }

    void initData() {
        userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
        if (null != userInfo && null != viewDataBinding) {
            viewDataBinding.setData(userInfo);
            viewDataBinding.executePendingBindings();
        }
        String imageSrc = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.IMAGE_ICON);
        //设置头像
        if (!TextUtils.isEmpty(imageSrc)) {
            CommonBindingAdapters.imageCircleHeadUrl(viewDataBinding.userHead, MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.IMAGE_ICON), true);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        UserInfo userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO,UserInfo.class);
        if(userInfo.userVipState==1){
            viewDataBinding.userVipIcon.setVisibility(View.VISIBLE);
        }else{
            viewDataBinding.userVipIcon.setVisibility(View.GONE);
        }

    }

    private void initView() {
        viewDataBinding.userSettingView.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), UserSettingActivity.class);
            getActivity().startActivity(intent);
        });

        viewDataBinding.userPersonView.setOnClickListener(v -> {
            ARouter.getInstance().build(RouterActivityPath.Room.PAGE_ROOM_PICKER).withInt("openType", 2).greenChannel().navigation();
        });

        viewDataBinding.userProtectPersonView.setOnClickListener(v -> {
            ARouter.getInstance().build(RouterActivityPath.Room.PAGE_ROOM_PICKER).withInt("openType", 1).greenChannel().navigation();
        });

        viewDataBinding.userCentry.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), UserSettingActivity.class);
            intent.putExtra(UserSettingActivity.OPEN_TYPE, 1);
            getActivity().startActivity(intent);
        });

        viewDataBinding.userHelpDocView.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), WebActivity.class);
            intent.putExtra(WebActivity.URL, UrlApi.explainUrl);
            intent.putExtra(WebActivity.TITLE, getString(R.string.user_help));
            getActivity().startActivity(intent);
        });

        viewDataBinding.userHelpView.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), WebActivity.class);
            intent.putExtra(WebActivity.URL, UrlApi.helpUrl);
            intent.putExtra(WebActivity.TITLE, getString(R.string.user_helpv));
            getActivity().startActivity(intent);
        });

        viewDataBinding.userVipCenter.setOnClickListener(v -> {
            //跳转VIP购买界面
            ARouter.getInstance()
                    .build(RouterActivityPath.Pay.PAGE_PAY_HOME)
                    .navigation();
        });

        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.USR_NAME_CHANGE, Integer.class)
                .observe(getViewLifecycleOwner(), temp -> {
                    initData();
                });

        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.IMAGE_ICON_CHANGE, Integer.class)
                .observe(getViewLifecycleOwner(), temp -> {
                    CommonBindingAdapters.imageCircleHeadUrl(viewDataBinding.userHead, MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.IMAGE_ICON), true);
                });

        UserInfo userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO,UserInfo.class);
        if(userInfo.userVipState==1){
           viewDataBinding.userVipIcon.setVisibility(View.VISIBLE);
        }else{
            viewDataBinding.userVipIcon.setVisibility(View.GONE);
        }

    }


    @Override
    public int getLayoutId() {
        return R.layout.user_main_fragment;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected ViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }

//    private void changeFragment(Fragment fragment){
//        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
//        fragmentTransaction.hide(this);
//        fragmentTransaction.add(fragment, fragment.getClass().getSimpleName());
//        fragmentTransaction.replace(((UserSettingActivity)getActivity()).getContentId(), fragment, fragment.getClass().getSimpleName());
//        fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
//        fragmentTransaction.commit();
//    }

    @Override
    public void loginStatusChanged() {
        if (null != viewDataBinding) {
            // 防止登录状态变更时, 还没有onViewCreated
            initData();
        }
    }
}
